<?php
/**
 * Copyright (C) 2009  Honghao Geng (genghonghao@gmail.com)
 * $Id: list.php 103 2009-07-31 17:39:44Z genghonghao $
 */
 !defined('SMARTGOON_MAIN_LOADED') ? define('SMARTGOON_MAIN_LOADED', 1) : NULL;
require_once './config.php';
require_once './includes/common.php';
require_once './includes/class.page.php';

$arr_replace = array('search', 'tag', 'category', 'filing');

$action = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : NULL;
$step   = isset($_REQUEST['step']) ? trim($_REQUEST['step']) : NULL;
if ($action === NULL || $step === NULL || !in_array($step, $arr_replace))
{
	redirect_page('��ȡ���ʧ��','������˷Ƿ���ֵ�����ݲ���ȷ',$root_url.'/index.php');
}

$where = '';
switch ($step)
{
	case 'search':
		$where = " subject LIKE '%{$action}%'";
		$nav = '��������:&nbsp;&nbsp'.$action;
	break;
	case 'tag' :
		$where = " tag_id LIKE '{$action}%' OR tag_id LIKE '%,{$action}%'";
		$nav = 'Tags����:&nbsp;&nbsp' .  get_name('tags', $action);
	break;
	case 'category' :
		$where = " category_id='{$action}'";
		$nav = '�����б�:&nbsp;&nbsp' . get_name('category', $action);;
	break;
	case 'filing' :
		$arr = explode('/', $action);
		$start = mktime(0, 0, 0, $arr[1], 1, $arr[0]);
		$last_day = date('t', $start);
		$end = mktime(12, 59, 59, $arr[1], $last_day, $arr[0]);
		$where = " submit_time>'{$start}' AND submit_time<'{$end}'";
		$nav = '�ļ��鵵:&nbsp;&nbsp;'.$action;
	break;
}
$tpl->assign('nav', $nav);

$temp = $db->result("SELECT COUNT(*) AS num FROM " . TB_ARTICLE . " WHERE " . $where);
$total = $temp[0]['num'];
$perpage = isset($GLOBALS['blog_page_num']) ? intval($GLOBALS['blog_page_num']) : 10;
if (!isset($_REQUEST['page']) || is_int($_REQUEST['page']))
{
	$p = 0;
}
else
{
	$p = ((int)$_REQUEST['page'] - 1) * $perpage;
	if ($p >= $total)
	{
		$p = 0;
	}
}

$result = $db->result("SELECT * FROM " . TB_ARTICLE . ' WHERE ' . $where . " ORDER BY article_id DESC LIMIT {$p}, {$perpage}");
$tpl->assign('list', $result);

require_once './header.php';
$page=new page(array('total'=>$total,'perpage'=>$perpage));
$tpl->assign('page', $page->show(4));
assign_right_list();
$tpl->display('list.html');
require_once './footer.php';
?>
